package com.excilys.formation.project.views;

import java.util.Map;
import java.util.SortedSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.excilys.formation.project.entities.Client;
import com.excilys.formation.project.entities.Operation;

@Component
public class ExcelView extends AbstractExcelView {

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		SortedSet<Operation> compteData = (SortedSet<Operation>) model.get("compteData");
		Client client = (Client) model.get("client");

		HSSFSheet sheet = workbook.createSheet("Rapport de Compte");

		HSSFRow headerrow = sheet.createRow(0);
		headerrow.setHeightInPoints(15);
		headerrow.createCell(0).setCellValue("Client: ");
		headerrow.createCell(1).setCellValue(client.getNom() + " " + client.getPrenom());

		HSSFRow title = sheet.createRow(2);
		title.createCell(0).setCellValue("Details Oprérations");

		HSSFRow header = sheet.createRow(4);
		header.createCell(0).setCellValue("Date");
		header.createCell(1).setCellValue("Type Oprération");
		header.createCell(2).setCellValue("Source");
		header.createCell(3).setCellValue("Montant");

		int rowNum = 5;
		for (Operation operation : compteData) {
			// create the row data
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(operation.getTransaction().getDate().toString());
			row.createCell(1).setCellValue(operation.getType().toString());
			row.createCell(2).setCellValue(operation.getTransaction().getLibelle());
			row.createCell(3).setCellValue(operation.getMontant());
		}

	}
}
